function getBookTypeList(){
    $.ajax({
        url: "/admin/getBookTypeList",
        type: "POST",
        success: function (bookTypes) {
            var tbody = document.querySelector("#datatables tbody");
            tbody.innerHTML = "";
            var rows = bookTypes.map(function (bookType) {
                return `
            <tr>
                <td>${bookType.typeId || ""}</td>
                <td>${bookType.typeName || ""}</td>
            </tr>
        `;
            });
            tbody.innerHTML = rows.join("");
        }
    });
}

$("#showPopup,#showPopup1").click(function () {
    $("#inputypeId").val("");
    $("#inputtypeName").val("");
    $("#inputypeId1").val("");
})

function getInfo(){
    let typeId = $("#inputypeId").val();
    $.ajax({
        url : "/admin/findBookTypeById",
        type : "POST",
        data : {
            typeId : typeId
        },
        success : function(bookType){
            if(bookType){
                $("#inputtypeName").val(bookType.typeName);
            }else {
                alert("图书类型不存在");
                $("#inputtypeName").val('');
            }
        },
        error : function(){
            alert("请求错误");
        }
    })
}

$("#btn-delete").click(function(event){
    event.preventDefault();
    $.ajax({
        url : "/admin/deleteBookType",
        type : "POST",
        data : {
            typeId : $("#inputypeId1").val()
        },
        success : function(res){
            if(res === "success"){
                alert("删除成功");
                getBookTypeList();
            }else{
                alert("删除失败");
            }
        },
        error : function(){
            alert("请求错误");
        }
    })
})

$("#btn-type-query").click(function (event) {
    event.preventDefault();
    let tbody = document.querySelector("#datatables tbody");
    $.ajax({
        url : '/admin/findBookTypeByInput',
        type : 'post',
        data : {
            typeId : $("#bookTypeId").val(),
            typeName : $("#typeName").val(),
        },
        success: function(bookTypeData){
            tbody.innerHTML = "";
            $.each(bookTypeData, function (index, bookType) {
                var row = $("<tr></tr>");
                var bookTypeIdTd = $("<td></td>").text(bookType.typeId);
                var bookTypeNameTd = $("<td></td>").text(bookType.typeName);
                row.append(bookTypeIdTd);
                row.append(bookTypeNameTd);
                $("#datatables tbody").append(row);
            });
        },
        error: function () {
            alert('请求发生错误，请稍后再试！');
        }
    })
})


